<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;

class LoginController extends BaseController
{
    public function index()
    {
        return view('login');
    }

    public function login(Request $request)
    {
        $username = $request->input('username');
        $password = $request->input('username');
        if (empty($username)) {
            return $this->fail('用户名不能为空');
        } else if (empty($username)) {
            return $this->fail('密码不能为空');
        } else {
            $user = DB::table('admins')->where("username", $username)->first();
            if ($user) {
                //验证验证码
                if (strcmp($user->password, md5($password)) == 0) {
                    return $this->success('登录成功');
                } else {
                    return $this->fail('密码错误');
                }
            } else {
                return $this->fail('用户不存在');
            }
        }
    }

    public function success($info = '', $data = [])
    {
        $return = [
            'data' => $data,
            'event' => 0,
        ];
        $return['info'] = $info;
        return response()->json($return);
    }

    public function fail($info)
    {
        $return = [
            'info' => $info,
            'status' => 1,
        ];
        return response()->json($return);
    }

}
